/**
 * Created by ajhin on 3/31/2015.
 */
package com.societyGames.flashForms.basicSkin
{
import com.societyGames.flashForms.DynamicTextField;

import flash.text.AntiAliasType;
import flash.text.GridFitType;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextLineMetrics;

public class SharedTextFields
{
  public static const LINK_STYLE:String = "a:link{color:#0000FF;text-decoration:underline;}a:hover{color:#444444;text-decoration:underline;}";
  public static const FONT_STYLE:String = "p{font-family:Open Sans;font-size:" + TEXT_SMALL_SIZE + ";}";
  public static const TEXT_COLOR:uint = 0x000000;
  public static const TEXT_LARGE_SIZE:int = 16;
  public static const TEXT_SMALL_SIZE:int = 12;

  /**
   * Adds CSS styling to a text field's existing styling.
   * @param textField
   * @param cssText
   */
  public static function addStyle(textField:TextField, cssText:String):void
  {
    var styleSheet:StyleSheet = textField.styleSheet;
    if (styleSheet == null)
    {
      styleSheet = new StyleSheet();
      textField.styleSheet = styleSheet;
    }
    styleSheet.parseCSS(cssText);
    textField.styleSheet = styleSheet;
  }

  /**
   * Makes a TextField use preferred render settings.
   * @param textField
   */
  public static function setDefaultRender(textField:TextField):void
  {
    textField.embedFonts = true;
    textField.antiAliasType = AntiAliasType.ADVANCED;
    textField.gridFitType = GridFitType.PIXEL;
    textField.useRichTextClipboard = true;
    textField.textColor = SharedTextFields.TEXT_COLOR;
  }

  public static function setDefaultFont(textField:TextField):void
  {
    var newFormat:TextFormat = new TextFormat();
    newFormat.font = "Open Sans";
    newFormat.size = TEXT_SMALL_SIZE;
    textField.defaultTextFormat = newFormat;
  }

  /**
   * Creates a basic TextField with correct font and rendering styles.
   * @return A basic TextField, non-html TextField.
   */
  public static function createTextField():TextField
  {
    var textField:TextField = new DynamicTextField();
    setDefaultRender(textField);
    setDefaultFont(textField);
    return textField;
  }

  /**
   * Creates a centered, single line text field.
   * @param text
   * @return
   */
  public static function createSingleLineTextField(text:String = ""):TextField
  {
    var textField:TextField = createTextField();
    var textFormat:TextFormat = textField.defaultTextFormat;
    textFormat.align = TextFormatAlign.CENTER;
    textField.defaultTextFormat = textFormat;

    textField.text = text;
    var metrics:TextLineMetrics = textField.getLineMetrics(0);
    textField.width = metrics.width + 4;
    textField.height = metrics.height + 4;
    return textField;
  }

  /**
   * Creates a basic HTML text field.
   * @return
   */
  public static function createHtmlTextField():TextField
  {
    var textField:TextField = createTextField();
    textField.condenseWhite = true;
    addStyle(textField, FONT_STYLE);
    addStyle(textField, LINK_STYLE);
    return textField;
  }
}
}
